package com.ztesoft.web.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.db.dao.DbBaseInfoDao;
import com.ztesoft.web.common.db.dao.DbLinkInstanceDao;
import com.ztesoft.web.common.db.po.DbBaseInfoPO;
import com.ztesoft.web.common.db.po.DbNodePO;
import com.ztesoft.web.common.db.po.SysModuleNodePO;
import com.ztesoft.web.common.service.IDbInfoService;
import com.ztesoft.web.common.vo.DbBaseInfoVO;
import com.ztesoft.web.common.vo.DbLinkInstanceVO;
import com.ztesoft.web.common.vo.DbUserInfoVO;
import com.ztesoft.web.syncbaseinfo.service.BaseInfoService;

/**
 * <Description> <br>
 * 
 * @author lin.jie<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2015年8月27日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.baseconfig.service.impl <br>
 */

@Service("dbInfoService")
public class DbInfoServiceImpl implements IDbInfoService {

	private static final ZTEsoftLogManager logger = ZTEsoftLogManager.getLogger(DbInfoServiceImpl.class);

	@Autowired
	private DbBaseInfoDao dbBaseInfoDao;
	@Autowired
	private DbLinkInstanceDao dbLinkInstanceDao;
	@Autowired
	private BaseInfoService baseInfoService;

	@Override
	public DbNodePO[] queryAllForTree() throws BaseAppException {
		List<DbBaseInfoPO> selectList = dbBaseInfoDao.selectAll();
		logger.debug("queryAllForTree : selectList = {0}", selectList);
		List<DbNodePO> dnPOList = new ArrayList<DbNodePO>();
		for (DbBaseInfoPO dbiPO : selectList) {
			DbBaseInfoVO dbiVO = baseInfoService.queryDBInfo(dbiPO.getDbBaseId());
			DbNodePO dnPO_db = new DbNodePO();
			dnPO_db.setId(dbiVO.getDbBaseId());
			dnPO_db.setpId(0);
			dnPO_db.setName(dbiVO.getDbName() + "(" + dbiVO.getDbHostIp() + ":" + dbiVO.getDbHostPort() + ")");
			dnPOList.add(dnPO_db);
			List<DbLinkInstanceVO> dbLinkInstanceList = dbiVO.getDbLinkInstance();
			for (DbLinkInstanceVO dliVO : dbLinkInstanceList) {
				DbNodePO dnPO_li = new DbNodePO();
				dnPO_li.setId(dliVO.getDbLinkId());
				dnPO_li.setpId(dbiVO.getDbBaseId() - (2 * dbiVO.getDbBaseId()));
				dnPO_li.setName(dliVO.getDbLinkName());
				dnPOList.add(dnPO_li);
				List<DbUserInfoVO> dbUserInfoList = dliVO.getDbUserInfo();
				for (DbUserInfoVO duiVO : dbUserInfoList) {
					DbNodePO dnPO_ui = new DbNodePO();
					dnPO_ui.setId(duiVO.getDbUserId());
					dnPO_ui.setpId(dliVO.getDbLinkId() - (2 * dliVO.getDbLinkId()));
					dnPO_ui.setName(duiVO.getDbUserName());
					dnPO_ui.setgId(dbiVO.getDbBaseId());
					dnPO_ui.setOpen(false);
					dnPOList.add(dnPO_ui);
				}
			}
		}
		// DbNodePO po_0 = new DbNodePO();
		// po_0.setName(selectList.get(0).getDbName());
		// po_0.setId(selectList.get(0).getpId());
		// po_0.setpId(0);
		// selectList.add(po_0);
		// for (int i = 0; i < listSize - 1; i++) {
		// if (selectList.get(i).getpId() == selectList.get(i + 1).getpId()) {
		// continue;
		// } else {
		// DbNodePO po = new DbNodePO();
		// po.setName(selectList.get(i + 1).getDbName());
		// po.setId(selectList.get(i + 1).getpId());
		// po.setpId(0);
		// selectList.add(po);
		// }
		// }
		return dnPOList.toArray(new DbNodePO[0]);
	}
}
